﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Microsoft.Practices.Unity;
using Vit.Common;
using Vit.Common.Extensions;
using Vit.Web.Providers;

namespace Vit.Web.Helpers
{
    public class DataSourceHelper
    {
        public static IEnumerable<SelectListItem> GetSourceList(string dataSourceName, string categoryName)
        {
            IDataSourceProvider dsp = GetDataSourceProvicer(dataSourceName);
            if (dsp == null)
                return null;
            return dsp.GetSourceList(categoryName);
        }

        public static string GetTextByValue(string dataSourceName, string categoryName, object value)
        {
            IDataSourceProvider dsp = GetDataSourceProvicer(dataSourceName);
            if (dsp == null)
                return null;
            return dsp.GetTextByValue(categoryName, value);
        }

        public static string GetValueByText(string dataSourceName, string categoryName, string text, bool insertIfNotExists)
        {
            IDataSourceProvider dsp = GetDataSourceProvicer(dataSourceName);
            if (dsp == null)
                return null;
            return dsp.GetValueByText(categoryName, text, insertIfNotExists);
        }

        public static IDataSourceProvider GetDataSourceProvicer(string dataSourceName)
        {
            return Unity.Current.Resolve<IDataSourceProvider>(dataSourceName);
        }
    }
}
